Skip to content

Git 纯本地仓库使用笔记

Git 纯本地仓库使用笔记(不绑定远端、不 push)

0. 进入项目目录

bash
cd /path/to/project

1. 初始化本地仓库

bash
git init

2. 配置提交身份(只需一次)

bash
git config --global user.name "YourName"
git config --global user.email "you@example.com"

3. 首次提交

bash
git add .
git commit -m "first commit"

日常使用流程(循环)

4. 查看当前状态

bash
git status

5. 查看具体改动(未暂存)

bash
git diff

6. 暂存改动

  • 暂存全部:
bash
git add .
  • 暂存单个文件:
bash
git add path/to/file

7. 查看已暂存改动

bash
git diff --staged

8. 提交

bash
git commit -m "your message"

查看历史与对比

9. 查看提交历史(简洁)

bash
git log --oneline --graph --decorate

10. 查看某次提交改了什么

  • 最新一次提交:
bash
git show HEAD
  • 指定提交(用提交哈希):
bash
git show <commit>

11. 对比“最新提交”和“上一次提交”

bash
git diff HEAD~1 HEAD

12. 对比任意两次提交

bash
git diff <commit1> <commit2>

13. 只看两次提交之间变动的文件列表

bash
git diff --name-only <commit1> <commit2>

撤销/回退(常用)

14. 撤销工作区改动(未 add)

  • 撤销单个文件:
bash
git restore path/to/file
  • 撤销全部:
bash
git restore .

15. 取消暂存(已 add,未 commit)

bash
git restore --staged .

16. 回退到上一次提交(保留改动在工作区)

bash
git reset --mixed HEAD~1

17. 回退到上一次提交(丢弃改动,谨慎)

bash
git reset --hard HEAD~1

18. 用新提交“撤销”某次提交(不改历史)

bash
git revert <commit>

确认没有绑定远端(可选)

bash
git remote -v